package sql

import (
	"database/sql"
	"fmt"

	_ "github.com/microsoft/go-mssqldb"
)

func Testsql() {
	connStr := "sqlserver://username:password@hostname?database=dbname"

	db, err := sql.Open("sqlserver", connStr)
	if err != nil {
		fmt.Println("Error opening database:", err)
		return
	}

	defer db.Close()

	err = db.Ping()
	if err != nil {
		fmt.Println("Error pinging database:", err)
		return
	}

	fmt.Println("Connected to the database!")

	rows, err := db.Query("select * from table")
	if err != nil {
		fmt.Println("Error executing query:", err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var itemcode string
		var itemname string
		err := rows.Scan(&itemcode, &itemname)
		if err != nil {
			fmt.Println("Error scanning row:", err)
			continue
		}
		fmt.Printf("ItemCode: %s, ItemName: %s\n", itemcode, itemname)
	}

	if err = rows.Err(); err != nil {
		fmt.Println("Error iterating over rows:", err)
	}
}
